package com.wind.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wind.weblog.common.domain.dos.CategoryDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface CategoryMapper extends BaseMapper<CategoryDO> {
    @Select("SELECT c.id, c.name, c.create_time, c.update_time, c.is_deleted, COUNT(acr.article_id) AS articlesTotal\n " +
            "FROM t_category c\n " +
            "LEFT JOIN t_article_category_rel acr ON c.id = acr.category_id\n " +
            "GROUP BY c.id, c.name, c.create_time, c.update_time, c.is_deleted\n")
    List<CategoryDO> selectCategoaryAndArticleInfo();

    @Select("SELECT c.id, c.name, c.create_time, c.update_time, c.is_deleted, COUNT(acr.article_id) AS articlesTotal\n " +
            "FROM t_category c\n " +
            "LEFT JOIN t_article_category_rel acr ON c.id = acr.category_id\n " +
            "WHERE c.id = #{categoryId}\n" +
            "GROUP BY c.id, c.name, c.create_time, c.update_time, c.is_deleted\n")
    CategoryDO selectCategoaryAndArticleInfoById(@Param("categoryId") Long categoryId);
}
